package _dp_base;

/**
 * 509. 斐波那契数
 */
public class No509 {
    /**
     * 2. 迭代
     */
    public int fib2(int n) {
        if (n < 1) return 0;
        int[] f = new int[n + 1];
        f[0] = 0;
        f[1] = 1;
        for (int i = 2; i < n + 1; i++) {
            f[i] = f[i - 1] + f[i - 2];
        }
        return f[n];
    }

    /**
     * 3. 空间优化
     */
    public int fib3(int n) {
        int f0 = 0, f1 = 1;
        for (int i = 0; i < n; i++) {
            int temp = f1;
            f1 = f0 + f1;
            f0 = temp;
        }
        return f0;
    }
}
